﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Fether;
using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;

namespace ExpenseSystem.Core.DeliveryModules
{
    public class DeliveryStateUpdateManager:DeliveryManager
    {
        int rows;
        DeliveryFetcher fetcher = new DeliveryFetcher();
        DeliveryDetailFetcher dFetcher = new DeliveryDetailFetcher();
        DeliveryManager orgOrderManager;
        DeliveryDetailManager odManager = new DeliveryDetailBaseManager();
        public DeliveryStateUpdateManager(DeliveryManager orderManager)
        {
            this.orgOrderManager = orderManager;
        }
        public override bool Approved(long id, out string message)
        {
            bool result = orgOrderManager.Approved(id, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }
        public override bool Comfirm(long id, out string message)
        {
            bool result = orgOrderManager.Comfirm(id, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }

        public override bool Delete(DeliveryModel Delivery, out string message)
        {
            return orgOrderManager.Delete(Delivery, out message);
        }

        public override bool DeleteById(long[] DeliveryId, out string message)
        {
            return orgOrderManager.DeleteById(DeliveryId, out message);
        }

        public override bool Recall(long modelid, out string message)
        {
            bool result = orgOrderManager.Recall(modelid, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }
        public override bool Reject(long id, out string message)
        {
            bool result = orgOrderManager.Reject(id, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            bool result = orgOrderManager.Revert(modelid, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }

        public override bool Save(DeliveryModel Delivery, out string message, params IAddable<DeliveryModel>[] addables)
        {
            return orgOrderManager.Save(Delivery,out message,addables);
        }

        public override bool Submit(long modelid, out string message)
        {
            bool result = orgOrderManager.Submit(modelid, out message);
            if (result)
            {
                IList<DeliveryDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (DeliveryDetailModel model in models)
                {
                    model.State = Definition.STATE_READY;
                    if (!odManager.Save(model, out message))
                        return false;
                }
            }
            return result;
        }
    }
}
